package com.ai.bitcoin.service.helper;

import com.ai.bitcoin.util.IdGeneration;
import com.ai.bitcoin.vo.BoomBustQueueVO;
import com.ai.bitcoin.vo.BuyerQueueVo;
import com.ai.bitcoin.vo.ErcTwoZeroQueueVO;
import com.tianru.bitcoin.search.api.domain.BoomBust;
import com.tianru.bitcoin.search.api.domain.BuyerSellerPoint;
import com.tianru.bitcoin.search.api.domain.ErcTwoZero;
import com.tianru.bitcoin.search.api.service.BoomBustService;
import com.tianru.bitcoin.search.api.service.BuyerSellerPointService;
import com.tianru.bitcoin.search.api.service.ErcTwoZeroService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @description: ${description}
 * @author: gulj
 * @create: 2018-08-12 19:04
 **/
@Component
@Slf4j
public class PushTemplateEsBizHelper {


    @Autowired
    private ErcTwoZeroService esErcTwoZeroService;

    @Autowired
    private BuyerSellerPointService esBuyerSellerPointService;

    @Autowired
    private BoomBustService boomBustService;


    /**
     * 保存Erc20信息到Es
     *
     * @param ercTwoZeroQueueVO
     * @return
     */
    public String saveEsErcTwoZero(ErcTwoZeroQueueVO ercTwoZeroQueueVO) {
        try {
            List<ErcTwoZero> txHash = esErcTwoZeroService.findTxHash(ercTwoZeroQueueVO.getTx_hash());
            if (null != txHash && !txHash.isEmpty()) {
                log.info("【保存erc20信息失败,erc20消息已经存在：{}】", txHash.get(0));
                return txHash.get(0).getTxHash();
            }
            ErcTwoZero ercTwoZero = new ErcTwoZero();
            ercTwoZero.setId(Long.valueOf(IdGeneration.getUniqueSign()));
            ercTwoZero.setBccId(ercTwoZeroQueueVO.getBcc_id());
            ercTwoZero.setToName(ercTwoZeroQueueVO.getTo_name());
            ercTwoZero.setVolume(ercTwoZeroQueueVO.getVolume());
            ercTwoZero.setFromName(ercTwoZeroQueueVO.getFrom_name());
            ercTwoZero.setToAddress(ercTwoZeroQueueVO.getTo_address());
            ercTwoZero.setFromAddress(ercTwoZeroQueueVO.getFrom_address());
            ercTwoZero.setTokenContactAddress(ercTwoZeroQueueVO.getToken_contract_address());
            ercTwoZero.setTxHash(ercTwoZeroQueueVO.getTx_hash());
            ercTwoZero.setTransferTime(ercTwoZeroQueueVO.getTransfer_time());
            ercTwoZero.setCirculationPercent(ercTwoZeroQueueVO.getCirculationPercent());
            return esErcTwoZeroService.saveEsErcTwoZero(ercTwoZero);
        } catch (Exception e) {
            log.error("【erc20 数据保存es失败，异常栈信息：{}】", e);
        }
        return null;
    }


    /**
     * 保存买点卖点到ES
     *
     * @param buyerQueueVo
     * @return
     */
    public Long saveEsBuyerSellPoint(BuyerQueueVo buyerQueueVo) {
        try {
            BuyerSellerPoint buyerSellerPoint = new BuyerSellerPoint();
            buyerSellerPoint.setId(Long.valueOf(IdGeneration.getUniqueSign()));
            buyerSellerPoint.setBccId(buyerQueueVo.getBcc_id());
            buyerSellerPoint.setKeyWord(buyerQueueVo.getKey_word());
            buyerSellerPoint.setCurrentRate(buyerQueueVo.getCurrent_rate());

            buyerSellerPoint.setTime(buyerQueueVo.getTime());
            buyerSellerPoint.setUpBound(buyerQueueVo.getUp_bound());
            buyerSellerPoint.setReachUp(buyerQueueVo.getReach_up());
            buyerSellerPoint.setLowBound(buyerQueueVo.getLow_bound());
            buyerSellerPoint.setReachLow(buyerQueueVo.getReach_low());
            return esBuyerSellerPointService.save(buyerSellerPoint);
        } catch (Exception e) {
            log.error("【买点卖点 数据保存es失败，异常栈信息：{}】", e);
        }
        return null;
    }


    /**
     * 保存暴涨暴跌到ES
     *
     * @param boomBustQueueVO
     * @return
     */
    public Long saveEsBoomBustQueueVO(BoomBustQueueVO boomBustQueueVO) {
        try {
            BoomBust boomBust = new BoomBust();
            BeanUtils.copyProperties(boomBust, boomBustQueueVO);
            boomBust.setId(Long.valueOf(IdGeneration.getUniqueSign()));
            return boomBustService.save(boomBust);
        } catch (Exception e) {
            log.error("【暴涨暴跌 数据保存es失败，异常栈信息：{}】", e);
        }
        return null;
    }


    /**
     * 根据Id从es中查询暴涨暴跌信息
     *
     * @param id
     * @return
     */
    public BoomBust findBoomBustByIdFromEs(Long id) {
        try {
            return boomBustService.findById(id);
        } catch (Exception e) {
            log.error("【暴涨暴跌 从ES中查询数据失败，异常栈信息：{}】", e);
        }
        return null;
    }


    /**
     * 根据Id从es中查询买点卖点信息
     *
     * @param id
     * @return
     */
    public BuyerSellerPoint findBuyerSellerPointByIdFromEs(Long id) {
        try {
            return esBuyerSellerPointService.findById(id);
        } catch (Exception e) {
            log.error("【买点卖点 从ES中查询数据失败，异常栈信息：{}】", e);
        }
        return null;
    }


    /**
     * 根据Id从es中查询erc20信息
     *
     * @param txHash
     * @return
     */
    public ErcTwoZero findErcTwoZeroByIdFromEs(String txHash) {
        try {
            List<ErcTwoZero> ercTwoZeros = esErcTwoZeroService.findTxHash(txHash);
            if (ercTwoZeros != null && !ercTwoZeros.isEmpty()) {
                return ercTwoZeros.get(0);
            }
        } catch (Exception e) {
            log.error("【erc20 从ES中查询数据失败，异常栈信息：{}】", e);
        }
        return null;
    }


    /**
     * 根据bccId和market获取最近价格
     *
     * @param bccId
     * @param market
     * @return
     */
    public String getLatestPriceByBccIdAndMarket(String bccId, String market) {
        List<BoomBust> boomBustList = null;
        try {
            boomBustList = boomBustService.findByBccIdAndMarket(bccId, market);
        } catch (Exception e) {
            log.error("【暴涨暴跌 从ES中查询价格信息失败，bccId 为：{}，market 为:{},异常栈信息：{}】", bccId, market, e);
            return null;
        }
        Double sum = 0.00;
        String result = null;
        if (null != boomBustList && !boomBustList.isEmpty()) {
            for (BoomBust boomBust : boomBustList) {
                String price = boomBust.getPrice();
                if (StringUtils.isNotEmpty(price)) {
                    Double aDouble = Double.valueOf(price);
                    sum = sum + aDouble;
                }
            }
            DecimalFormat df = new DecimalFormat("0.00");
            result = df.format(sum);

        }
        return result;
    }


}
